﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Data.SqlClient;
namespace NCKH.Admin
{
    public partial class suadanhmuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hientennhomtin();

                if (Request.QueryString["pid"] != null)
                {
                    int nhomtinID = Convert.ToInt32(Request.QueryString["pid"]);
                    hiennhomtin(nhomtinID);
                }

            }
        }



        protected void hiennhomtin(int nhomtinID)
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string select_danhmuc = "SELECT Madanhmuctin,NHOMTIN.Manhomtin, Tennhomtin, Tendanhmuctin FROM DANHMUCTIN, NHOMTIN WHERE DANHMUCTIN.Manhomtin=NHOMTIN.Manhomtin and Madanhmuctin='" + nhomtinID + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(select_danhmuc, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows && rd.Read())
                        {
                            txtmadanhmuc.Text = rd["Madanhmuctin"].ToString();
                            drdtennhom.SelectedValue = rd["Manhomtin"].ToString();
                            txttendanhmuc.Text = rd["Tendanhmuctin"].ToString();
                            btnsua.CommandArgument = nhomtinID.ToString();
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }

        protected void hientennhomtin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Manhomtin, Tennhomtin FROM NHOMTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                drdtennhom.Items.Add(
                                    new ListItem(rd["Tennhomtin"].ToString(), rd["Manhomtin"].ToString()));
                            }
                        else
                            btnsua.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }


        protected void btnsua_Click(object sender, EventArgs e)
        {
            int @Madanhmuctin = Convert.ToInt32(btnsua.CommandArgument);
            int catID = Convert.ToInt32(drdtennhom.SelectedValue);



            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string update = "UPDATE DANHMUCTIN SET Manhomtin='" + catID + "', Tendanhmuctin=@Tendanhmuctin Where Madanhmuctin='" + @Madanhmuctin + "'";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(update, cnn))
                {

                    cmd.CommandType = CommandType.Text;
                    // cmd.Parameters.AddWithValue("Madanhmuctin", btnsua.CommandArgument);
                    // cmd.Parameters.AddWithValue("Manhomtin", catID);
                    cmd.Parameters.AddWithValue("@Tendanhmuctin", txttendanhmuc.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();
                }
            }
            Response.Redirect("danhmuctin.aspx");
        }



        protected void btnhuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("danhmuctin.aspx");
        }

        protected void txttendanhmuc_TextChanged(object sender, EventArgs e)
        {

        }
    }
}